package com.emailsystem.emailsystem.service.impl;

import com.emailsystem.emailsystem.dao.Impl.UserDAO;
import com.emailsystem.emailsystem.domain.Completion;
import com.emailsystem.emailsystem.domain.Connection;
import com.emailsystem.emailsystem.domain.User;
import com.emailsystem.emailsystem.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDAO userDAO;

    private  User currentUser=new User();

    @Override
    public String getPassword(String account) {
        return userDAO.selectPasswordByAccount(account);
    }

    @Override
    public User getUserByAccount(String account) {
        return userDAO.selectUserByAccount(account);
    }

    @Override
    public void insert(String account, String password) {
        userDAO.insert(account,password);
    }

    @Override
    public void update(String account, String password) {
        userDAO.update(account,password);
    }

    // 生成唯一ID
    private static String generateUniqueUserID(String account, String timestamp) {
        // 将账号和时间戳拼接起来
        String combinedString = account + timestamp;

        // 获取拼接字符串的哈希码
        int hashCode = combinedString.hashCode();

        // 将哈希码转换为正数
        long positiveHashCode = Math.abs((long) hashCode);

        // 将正数哈希码截取为10位长度的字符串
        String userID = String.format("%010d", positiveHashCode);

        return userID;
    }

//    @Override
//    public boolean insert(String nickname,String account,String password) {
//        //截取当前时间+account的一部分，共10位
//        // 获取当前时间
//        LocalDateTime currentTime = LocalDateTime.now();
//        // 格式化时间为字符串
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
//        String timestamp = currentTime.format(formatter);
//        String userID = generateUniqueUserID(account, timestamp);
//        // 测试
//        System.out.println("Generated UserID: " + userID);
//        return userDAO.insertCompletion(userID, nickname, account, password);
//    }

    @Override
    public boolean updateConnection(String account, String IP, Integer port,String protocol) {
       return userDAO.updateConnetion(account, IP, port,protocol);
    }

    @Override
    public boolean addConnection(String account, String IP, Integer port, String protocol) {
        //截取当前时间+account的一部分，共10位
        // 获取当前时间
        LocalDateTime currentTime = LocalDateTime.now();
        // 格式化时间为字符串
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String timestamp = currentTime.format(formatter);
        String connectionID = generateUniqueUserID(account, timestamp);
        return userDAO.insertConnection(connectionID,account,IP,port,protocol);
    }

    @Override
    public ArrayList<Connection> getConnection(String account) {
        return userDAO.getConnectionByAccount(account);
    }

    public boolean testAccountExistence(String account){
        return userDAO.testUserByAccount(account) == 1;
    }

    @Override
    public Completion getCompletionByAccount(String account) {
        return userDAO.getCompletionByAccount(account);

    }

    @Override
    public User getCurrentUser() {
        return this.currentUser;

    }

    @Override
    public void setCurrentUser(String Account) {
        this.currentUser.setAccount(Account);

    }
}
